package peace.grapher
{
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Point;

import peace.data.Constraints;
import peace.data.Interval;
import peace.grapher.chart.Chart;
import peace.grapher.chart.Curve;
import peace.gui.extended.RubberSprite;
/**
 * Координатная плоскость
 * 
 * 
 */
public class CoordinatePlane extends RubberSprite
{
	/**
	 * 
	 */
	public var xAxis:Axis = new Axis(true);
	/**
	 * 
	 */
	public var yAxis:Axis = new Axis(false);
	/**
	 * 
	 */
	public var grid:Grid;
	/**
	 * контейнер для чартов
	 */	
	protected var charts:Sprite = new Sprite();
	/**
	 * 
	 */
	protected var curvesMask:Shape = new Shape();
	/**
	 * 
	 * 
	 * @param area
	 * @param grid
	 * 
	 */
	public function CoordinatePlane (area:Constraints = null, grid:Grid = null)
	{
		super();
		
		this.grid = grid;
		if (grid) addChild(grid);
		
		xAxis.hasNil = false;
		if (area)
		{
			xAxis.interval.min = area.left;
			xAxis.interval.max = area.right;
		}
 		addChild(xAxis);
		size.width = xAxis.size;
		
		yAxis.hasNil = false;
		if (area)
		{
			yAxis.interval.min = area.bottom;
			yAxis.interval.max = area.top;
		}
		addChild(yAxis);
		size.height = yAxis.size;
		
		with (curvesMask.graphics)
		{
			clear();
			beginFill(0,0.5);
			drawRect(0,0,1,1);
		}
		
		addChild(curvesMask)
		charts.mask = curvesMask;
		addChild(charts);
		
		mouseEnabled = false;
	}
	/**
	 * Основной инструмент отрисовки 
	 * 
	 */	
	public function redraw ():void
	{
		xAxis.redraw();
		yAxis.redraw();
		
		// двигаем оси в (0,0)
		var i:Interval = yAxis.interval;
		xAxis.y = yAxis.interval.contains(0) ? yAxis.getGlobalValue(0) : (i.min > 0 ? yAxis.size : 0);
		i = xAxis.interval;
		yAxis.x = i.contains(0) ? xAxis.getGlobalValue(0) : (i.min > 0 ? 0 : xAxis.size);
		
		// перерисовываем сетку если есть
		if (grid) grid.redraw(this);
		
		// чарты
		if (charts.numChildren > 0)
		{
			var j:int;
			var c:Chart;
			for (j = 0; j < charts.numChildren; j++)
			{
				c = Chart(charts.getChildAt(j));
				c.redraw();
			}
		}
	}
	/**
	 * 
	 * 
	 */	
	override public function arrange():void
	{
		if (widthChanged || heightChanged)
		{
			xAxis.size = size.width;
			yAxis.size = size.height;
			
			// скейлим маску кривых 
			curvesMask.width = size.width;
			curvesMask.height = size.height;
			
//			redraw();
		}
		super.arrange();
	}
	/**
	 * 
	 * @param c
	 * 
	 */	
	public function addChart (c:Chart):void
	{
		charts.addChild (c);
	}
	/**
	 * 
	 * @param c
	 * 
	 */	
	public function removeChart (c:Chart):void
	{
		var d:DisplayObject = DisplayObject(c);
		if (c.parent && c.parent == charts)
		{
			charts.removeChild (c);
		}
	}
	/**
	 * 
	 * @param local
	 * @return 
	 * 
	 */	
	public function getGlobalPoint (local:Point):Point
	{
		return new Point (xAxis.getGlobalValue(local.x), yAxis.getGlobalValue(local.y));
	}
	/**
	 * 
	 * @param global
	 * @return 
	 * 
	 */	
	public function getLocalPoint (global:Point):Point
	{
		return new Point (xAxis.getLocalValue(global.x), yAxis.getLocalValue(global.y));
	}
	
	
}
}